package com.hl.util.KL.Encryption.ECC;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.google.gson.Gson;

class EccOption {
	private static EccOption _instance = null;

	static EccOption GetInstance(String host) throws MalformedURLException,
			IOException {
		if (_instance == null) {
			_instance = new EccOption(host);
		}
		return _instance;
	}

	private boolean isEnabled = false;

	boolean enabled() {
		return isEnabled;
	}

	BigInteger Prime;
	BigInteger A;
	BigInteger B;
	BigPoint G;
	BigInteger PrivateKey;
	BigPoint PublicKey;
	@SuppressWarnings("unused")
	private String strPublicKey = "AgzDml7DsDHCrjgZKXMJwojCvg==";

	EccOption(String host) throws MalformedURLException, IOException {
		HttpURLConnection connection = (HttpURLConnection) new URL(host
				+ "/encrypt/getparams").openConnection();

		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(connection.getInputStream()));
		StringBuffer strBuffer = new StringBuffer();
		String line = null;
		while ((line = bufferedReader.readLine()) != null) {
			strBuffer.append(line);
		}
		try {
			bufferedReader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		Gson gson = new Gson();
		KLEccParams p = gson.<KLEccParams> fromJson(strBuffer.toString(),
				KLEccParams.class);
		isEnabled = p.Enabled;
		A = new BigInteger(p.A);
		B = new BigInteger(p.B);
		int idx = p.G.indexOf(",");
		G = new BigPoint();
		G.Infinity = true;
		G.X = new BigInteger(p.G.substring(0, idx));
		G.Y = new BigInteger(p.G.substring(idx + 1));
		G.Infinity = false;
		Prime = new BigInteger(p.Prime);
		PublicKey = new BigPoint();
		idx = p.PublicKey.indexOf(",");
		PublicKey.Infinity = true;
		PublicKey.X = new BigInteger(p.PublicKey.substring(0, idx));
		PublicKey.Y = new BigInteger(p.PublicKey.substring(idx + 1));
		PublicKey.Infinity = false;
		// PublicKey=new
		// EccCalc().StringToPoint(stringExtend.DecodeBase64String(strPublicKey),
		// Prime, A, B);
	}
}
